﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class Utility : MonoBehaviour
{
    #region 卡牌逻辑相关
    // 每张相同花色，总分加100
    // 返回最高总分
    public static int ScoreOfCardGroup(List<int> cards)
    {
        int[] countOfType = new int[5] { 0, 0, 0, 0, 0 };
        int[] scoreOfType = new int[5] { 0, 0, 0, 0, 0 };
        for (var i = 0; i < cards.Count; i++)
        {
            countOfType[TypeOfCard(cards[i])]++;
            scoreOfType[TypeOfCard(cards[i])] += ScoreOfCard(cards[i]);
        }
        // 遍历所有牌型的分数
        int maxScore = 0;
        for (var i = 0; i < 4; i++)
        {
            var s = scoreOfType[i] + countOfType[i] * 100; // 100是对牌数的加权
            if ( s > maxScore)
            {
                maxScore = s;
            }
        }
        return maxScore + countOfType[4] * 110; // 一张王牌是100加权分和10分牌面分
    }
    
    // 卡牌的类型
    public static int TypeOfCard(int code)
    {
        if (code > 51)
            return 4;
        return code / 13;
    }
    // 卡牌的分数
    public static int ScoreOfCard(int code)
    {
        // joker
        if (code > 51)
            return 10;
        // 获得点数
        int t = code % 13;
        // Ace
        if (t == 0)
            return 11;
        // 2~10(code为1~9)
        if (t < 10)
            return t + 1;
        // J Q K
        return 10;
    }
    #endregion

    public static string ToString(List<int> l)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < l.Count; i++)
        {
            sb.Append(CardName(l[i]));
            sb.Append(" ");
        }
        sb.Append("\n");
        return sb.ToString();
    }

    public static string CardGroupString(List<int> cardGroup)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < cardGroup.Count; i++)
        {
            sb.Append(CardName(cardGroup[i])+ "</color> ");
        }
        return sb.ToString();
    }

    public static string CardName(int code)
    {
        string name = null;
        if (code == 54)
            name = "牌背";
        else if (code == 53)
            name = "大王";
        else if (code == 52)
            name = "小王";
        else
        {
            int type = code / 13;
            var codeName = string.Format("{0:D}", code % 13 + 1);
            switch(code%13)
            {
                case 0:
                    codeName = "A";
                    break;
                case 10:
                    codeName = "J";
                    break;
                case 11:
                    codeName = "Q";
                    break;
                case 12:
                    codeName = "K";
                    break;
            }
            switch (type)
            {
                case 0:
                    name = "<color=red>♥" + codeName;
                    break;
                case 1:
                    name = "<color=black>♠" + codeName;
                    break;
                case 2:
                    name = "<color=black>♣" + codeName;
                    break;
                case 3:
                    name = "<color=red>♦" + codeName;
                    break;
            }
        }
        return name;
    }
}
